﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI;
using FriendAnalyzer.Business;
using FriendAnalyzer.Infrastructure.Files;
using System.Threading.Tasks;
namespace FriendAnalyzer
{
    public sealed partial class MainPage : Page
    {
        private Dictionary<string, BitmapImage> _images;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async Task<Dictionary<string, BitmapImage>> GetImagesAsync()
        {
            if (_images == null)
            {
                var manager = new FileManager();

                var cachedImages = await manager.GetImagesAsync();

                _images = cachedImages.ToDictionary(i => System.IO.Path.GetFileNameWithoutExtension(i.Item1), i => i.Item2);
            }

            return _images;
        }

        Graph.Diagram mDiagram;
        Random mRandom;
        private AggregateService service;
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            cnvDrawing.Height = Window.Current.Bounds.Height;
            cnvDrawing.Width = Window.Current.Bounds.Width - spSideBar.Width;
            pbStatus.Visibility = Visibility.Collapsed;
            service = e.Parameter as AggregateService;
        }
       
        private async void btnDraw_Click_1(object sender, RoutedEventArgs e)
        {
            pbStatus.Visibility =  Visibility.Visible;
            cnvDrawing.Opacity = 0.2;
           if(chkSameEvents.IsChecked.Value)
            service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.EventAggregationStrategy());
           if (chkLikeSamePages.IsChecked.Value)
            service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.LikesAggregationStrategy());
           if (chkCheckIns.IsChecked.Value)
               service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.CheckInAggregationStrategy());
           if (chkBelongToGroups.IsChecked.Value)
               service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.GroupAggregationStrategy());
           if (chkSameGender.IsChecked.Value)
               service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.GenderAggregationStrategy());
            var graph = await Graph.StatisticDataConverter.BuildGraphNodes((uint)sldClusters.Value,service,false);
            graph.ATTRACTION_CONSTANT = sldAttraction.Value / 1000;
            graph.REPULSION_CONSTANT = sldRepulsion.Value * 1000;
            graph.Arrange(true);
            //graph.Arrange(0.8,50,30,true);
            var bounds = new Graph.Rectangle(0.0, 0.0, cnvDrawing.Width, cnvDrawing.Height);

            var images = await GetImagesAsync();

            graph.Draw(cnvDrawing, bounds, images);

            pbStatus.Visibility = Visibility.Collapsed;
            cnvDrawing.Opacity = 1;
        }

        private async void btnRerun_Click_1(object sender, RoutedEventArgs e)
        {
            pbStatus.Visibility =Visibility.Visible;
            cnvDrawing.Opacity = 0.2;
            if (chkSameEvents.IsChecked.Value)
                service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.EventAggregationStrategy());
            if (chkLikeSamePages.IsChecked.Value)
                service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.LikesAggregationStrategy());
            if (chkCheckIns.IsChecked.Value)
                service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.CheckInAggregationStrategy());
            if (chkBelongToGroups.IsChecked.Value)
                service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.GroupAggregationStrategy());
            if (chkSameGender.IsChecked.Value)
                service.AddFacebookFilter(new FriendAnalyzer.Business.FacebookFilter.GenderAggregationStrategy());
            var graph = await Graph.StatisticDataConverter.BuildGraphNodes((uint)sldClusters.Value, service,true);
            graph.ATTRACTION_CONSTANT = sldAttraction.Value / 1000;
            graph.REPULSION_CONSTANT = sldRepulsion.Value * 1000;
            graph.Arrange(true);
            var bounds = new Graph.Rectangle(0.0, 0.0, cnvDrawing.Width, cnvDrawing.Height);
            var images = await GetImagesAsync();
            graph.Draw(cnvDrawing, bounds, images);
            pbStatus.Visibility = Visibility.Collapsed; cnvDrawing.Opacity = 1;
            btnDraw.Visibility = Windows.UI.Xaml.Visibility.Visible;
        }
       
    }
}
